package com.accompany.controller;

import com.accompany.common.Result;
import com.accompany.entity.Transaction;
import com.accompany.service.TransactionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 交易记录表 前端控制器
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/transaction")
@Tag(name = "TransactionController", description = "交易记录管理")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    /**
     * 创建交易记录
     *
     * @param transaction 交易记录信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建交易记录", description = "创建一个新的交易记录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "交易记录创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数不正确")
    })
    public Result<?> createTransaction(@RequestBody Transaction transaction) {
        transactionService.createTransaction(transaction);
        return Result.success();
    }

    /**
     * 删除交易记录
     *
     * @param id 交易记录ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除交易记录", description = "根据交易记录ID删除交易记录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "交易记录删除成功"),
            @ApiResponse(responseCode = "404", description = "交易记录不存在")
    })
    public Result<?> deleteTransaction(@PathVariable Long id) {
        transactionService.deleteTransaction(id);
        return Result.success("删除成功");
    }

    /**
     * 更新交易记录信息
     *
     * @param transaction 交易记录信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新交易记录信息", description = "根据交易记录ID更新交易记录信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "交易记录信息更新成功"),
            @ApiResponse(responseCode = "404", description = "交易记录不存在")
    })
    public Result<?> updateTransaction(@RequestBody Transaction transaction) {
        transactionService.updateTransaction(transaction);
        return Result.success("更新成功");
    }

    /**
     * 获取单个交易记录信息
     *
     * @param id 交易记录ID
     * @return Result对象，包含交易记录信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取单个交易记录信息", description = "根据交易记录ID获取交易记录信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "交易记录不存在")
    })
    public Result<Transaction> getTransactionById(@PathVariable Long id) {
        Transaction transaction = transactionService.getTransactionById(id);
        return Result.success(transaction);
    }

    /**
     * 获取所有交易记录列表
     *
     * @return Result对象，包含交易记录列表
     */
    @GetMapping
    @Operation(summary = "获取所有交易记录列表", description = "获取所有交易记录的列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<List<Transaction>> getAllTransactions() {
        List<Transaction> transactions = transactionService.getAllTransactions();
        return Result.success(transactions);
    }

    /**
     * 分页查询交易记录列表
     *
     * @param page  页码
     * @param size  每页大小
     * @return Result对象，包含分页后的交易记录列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询交易记录列表", description = "根据页码和每页大小分页查询交易记录列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "分页查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<Page<Transaction>> getPaginatedTransactions(
            @Parameter(description = "页码", required = true) @RequestParam(name = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = true) @RequestParam(name = "size", defaultValue = "10") Integer size) {
        Page<Transaction> pageResult = transactionService.getPaginatedTransactions(page, size);
        return Result.success(pageResult);
    }
}